package cn.findviewbydream.evil.bigtoutiao.utils;

import com.google.gson.FieldNamingPolicy;
import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import com.google.gson.stream.JsonReader;
import com.lzy.okgo.callback.AbsCallback;

import java.lang.reflect.ParameterizedType;
import java.lang.reflect.Type;

import okhttp3.Response;

/********************************************
 * 工程名：MealOrderOfWaiter
 * 包名：cn.findviewbydream.evil.mealorderofwaiter.utils
 * 时间：16-12-23
 * 作者：白雨濃 evil
 * 邮箱：evilbai@foxmail.com
 * 内容：
 ********************************************/
public abstract class GsonCallBack<T> extends AbsCallback<T>
{
    @Override
    public T convertSuccess(Response response) throws Exception
    {
        Gson gson = new GsonBuilder()
                .setFieldNamingPolicy(FieldNamingPolicy.LOWER_CASE_WITH_UNDERSCORES)
                .setDateFormat("yyyy-MM-dd HH:mm:ss").create();

        Type genType = getClass().getGenericSuperclass();
        Type[] params = ((ParameterizedType) genType).getActualTypeArguments();
        Type type = params[0];
        JsonReader jsonReader = new JsonReader(response.body().charStream());
        T data = gson.fromJson(jsonReader, type);
        response.close();
        return data;
    }
}